package com.demo.task;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.net.URL;

/**
 * Created by VietBui on 11/30/13.
 */
public abstract class TaskDownload extends  Thread{
    private static final int IMAGE_BITMAP = 1;
    private final String urlImage;
//    http://fc09.deviantart.net/fs71/i/2012/034/2/8/sexy_girl_by_dgeblade-d4ohx6w.jpg
    private Handler handler;

    public TaskDownload(Handler handler,String urlImage){
        this.handler = handler;
        this.urlImage = urlImage;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(urlImage);
            final Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//            Message message = new Message();
//            message.what = IMAGE_BITMAP;
//            message.obj = image;
//            handler.sendMessage(message);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onDownloadFinish(image);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public abstract void onDownloadFinish(Bitmap bitmap);
}
